package com.yangrong.newsservice.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yangrong.newsservice.entity.TbArticlesEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 文章表
 *
 * @author yangrong
 * @email 455833224@qq.com
 * @date 2020-09-04 11:09:13
 */
@Mapper
public interface TbArticlesDao extends BaseMapper<TbArticlesEntity> {

    /**
     * 加载更多文章（可区分类型、标签）
     *
     * @param type    文章类型
     * @param label   文章标签
     * @param start   查询起始位置
     * @param getSize 查询数量
     * @return
     */
    @Select("<script>" +
            "select t.* " +
            "from tb_articles t " +
            "where 1 = 1 " +
            "<if test='type != null and type.trim() != &quot;&quot; and type.trim() != 0'>" +
            "AND t.type = #{type} " +
            "</if>" +
            "<if test='label != null and label.trim() != &quot;&quot; and label.trim() != 0'>" +
            "AND t.label like CONCAT('%|',#{label},'|%') " +
            "</if>" +
            "<if test='start != null and getSize != null'>" +
            "limit #{start}, #{getSize}" +
            "</if>" +
            "</script>")
    List<TbArticlesEntity> getMoreArticleType(String type, String label, Integer start, Integer getSize);

    /**
     * 获取文章列表
     *
     * @param type  分类
     * @param label 标签
     * @return
     */
    @Select("<script>" +
            "select t.* " +
            "from tb_articles t " +
            "where 1 = 1 " +
            "<if test='type != null and type.trim() != &quot;&quot; and type.trim() != 0'>" +
            "AND t.type = #{type} " +
            "</if>" +
            "<if test='label != null and label.trim() != &quot;&quot; and label.trim() != 0'>" +
            "AND t.label like CONCAT('%|',#{label},'|%') " +
            "</if>" +
            "limit 0, 10 " +
            "</script>")
    List<TbArticlesEntity> getArticleList(String type, String label);

    @Select("<script>" +
            "select count(1) " +
            "from tb_articles t " +
            "where 1 = 1 " +
            "<if test='type != null and type.trim() != &quot;&quot; and type.trim() != 0'>" +
            "AND t.type = #{type} " +
            "</if>" +
            "<if test='label != null and label.trim() != &quot;&quot; and label.trim() != 0'>" +
            "AND t.label like CONCAT('%|',#{label},'|%') " +
            "</if>" +
            "</script>")
    Integer getArticleListSize(String type, String label);
}
